Author: SAI K
Core Java
In Java, primitive data types are the most basic data types that represent simple values. These data types are predefined by the language and serve as the building blocks for data manipulation. Java has eight primitive data types, each serving a specific purpose and having its own range of values.
Primitive data types are the simplest and most fundamental data types available in Java. They are not objects and hold their values directly in memory. Primitive data types are efficient and provide a way to create variables to store data of different types.
Java defines eight primitive data types:
The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive).
Example:
public class ByteExample {
public static void main(String[] args) {
byte b = 100;
System.out.println("Byte value: " + b);
}
}
The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).
Example:
public class ShortExample {
public static void main(String[] args) {
short s = 10000;
System.out.println("Short value: " + s);
}
}
The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2^31 and a maximum value of 2^31 - 1 (inclusive).
Example:
public class IntExample {
public static void main(String[] args) {
int i = 100000;
System.out.println("Int value: " + i);
}
}
The long data type is a 64-bit signed two's complement integer. It has a minimum value of -2^63 and a maximum value of 2^63 - 1 (inclusive).
Example:
public class LongExample {
public static void main(String[] args) {
long l = 100000L;
System.out.println("Long value: " + l);
}
}
The float data type is a single-precision 32-bit IEEE 754 floating point. It is mainly used to save memory in large arrays of floating point numbers.
Example:
public class FloatExample {
public static void main(String[] args) {
float f = 10.5f;
System.out.println("Float value: " + f);
}
}
The double data type is a double-precision 64-bit IEEE 754 floating point. For decimal values, this data type is generally the default choice.
Example:
public class DoubleExample {
public static void main(String[] args) {
double d = 10.5;
System.out.println("Double value: " + d);
}
}
The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
Example:
public class CharExample {
public static void main(String[] args) {
char c = 'A';
System.out.println("Char value: " + c);
}
}
The boolean data type has only two possible values: true
and false
. This data type is used for simple flags that track true/false conditions.
Example:
public class BooleanExample {
public static void main(String[] args) {
boolean bool = true;
System.out.println("Boolean value: " + bool);
}
}
Primitive data types in Java are essential for efficient data manipulation and storage. They provide the foundation for handling basic data types and serve as the building blocks for more complex data structures. Understanding and using these primitive data types correctly is crucial for effective Java programming.